package offer;

import java.util.ArrayList;
import java.util.List;

/*
P147
* */
public class CopyComplexList {

    public class ComplexListNode{
        int value;
        ComplexListNode pNext;
        ComplexListNode pSibling;
    }

    public ComplexListNode clone(ComplexListNode pHead){
        copyMain(pHead);
        copySibling(pHead);
        return splitSibling(pHead);
    }

    public void copyMain(ComplexListNode pHead){
        ComplexListNode point = pHead;
        while(point != null){
            ComplexListNode node = new ComplexListNode();
            node.value = point.value;
            node.pNext = point.pNext;
            point.pNext = node;
            point = node.pNext;
        }
    }

    public void copySibling(ComplexListNode pHead){
        ComplexListNode point = pHead;
        while(point != null){
            ComplexListNode copyNode = point.pNext;
            ComplexListNode pSibling = point.pSibling;
            if(pSibling != null){
                copyNode.pSibling = pSibling.pNext;
            }
            point = copyNode.pNext;
            List<String> a = new ArrayList<String>();
        }
    }

    public ComplexListNode splitSibling(ComplexListNode pHead){
        ComplexListNode pClone = null;
        ComplexListNode point = pHead;
        ComplexListNode node = null;
        if(point != null){
            pClone = point.pNext;
            node = pClone;
            point.pNext = node.pNext;
            point = node.pNext;
        }
        while(point != null){

            node.pNext = point.pNext;
            node = point.pNext;
            point.pNext = node.pNext;
            point = node.pNext;
        }

        return pClone;
    }


}
